<template>
  <div class="app-container">
    <div v-permiss="['60208630']" class="filterDiv tx-r">
      <el-button size="small" class="filter-item" round type="primary" @click="handleSync">恢复系统默认</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" :data="tableData" size="small" fit highlight-current-row style="width: 100%;">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="项目名称" prop="cashFlowName" align="left" min-width="140" />
        <el-table-column label="行次" prop="lineNo" align="center" width="50" />
        <el-table-column label="项目类型" prop="type" align="center" width="100">
          <template slot-scope="{row}">
            <span v-if="row.type === 0">标题</span>
            <span v-if="row.type === 1">分录数据</span>
            <span v-if="row.type === 2">科目余额</span>
            <span v-if="row.type === 3">表内计算</span>
          </template>
        </el-table-column>
        <el-table-column label="借方科目" prop="drCoaCodes" align="left">
          <template slot-scope="{row}">
            <span style="white-space:break-word">{{ row.drCoaCodes && row.drCoaCodes.replace(/,/g, '、') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="借方忽略科目" prop="drExceptedCoaCodes" align="left">
          <template slot-scope="{row}">
            <span style="white-space:break-word">{{ row.drExceptedCoaCodes && row.drExceptedCoaCodes.replace(/,/g, '、') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="贷方科目" prop="crCoaCodes" align="left">
          <template slot-scope="{row}">
            <span style="white-space:break-word">{{ row.crCoaCodes && row.crCoaCodes.replace(/,/g, '、') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="贷方忽略科目" prop="crExceptedCoaCodes" align="left">
          <template slot-scope="{row}">
            <span style="white-space:break-word">{{ row.crExceptedCoaCodes && row.crExceptedCoaCodes.replace(/,/g, '、') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permiss="['60208610']" label="操作" align="center" width="80">
          <template slot-scope="{row}">
            <el-button v-if="row.type !== 0" type="text" size="small" @click="handleShow(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="false" title="修改现金流量项目" :visible.sync="dialogVisible" width="600px">
      <el-form ref="dataForm" :model="form" label-position="right" :label-width="rowType===1?'100px':'70px'" style="margin-left:10px;width: 550px;">
        <el-form-item label="借方科目">
          <template v-if="rowType !== 2">
            <el-input v-model="form.drCoaCodes" type="textarea" placeholder="借方科目" />
          </template>
          <template v-else>
            <el-radio v-model="form.drCoaCodes" label="BEGIN_BALANCE" style="margin-right:60px;margin-left:20px">期初余额</el-radio>
            <el-radio v-model="form.drCoaCodes" label="END_BALANCE">期末余额</el-radio>
          </template>
        </el-form-item>
        <el-form-item v-if="rowType === 1" label="借方忽略科目">
          <el-input v-model="form.drExceptedCoaCodes" type="textarea" placeholder="借方忽略科目" />
        </el-form-item>
        <el-form-item label="贷方科目">
          <template v-if="rowType !== 2">
            <el-input v-model="form.crCoaCodes" type="textarea" placeholder="借方科目" />
          </template>
          <template v-else>
            <el-radio v-model="form.crCoaCodes" label="BEGIN_BALANCE" style="margin-right:60px;margin-left:20px">期初余额</el-radio>
            <el-radio v-model="form.crCoaCodes" label="END_BALANCE">期末余额</el-radio>
          </template>
        </el-form-item>
        <el-form-item v-if="rowType === 1" label="贷方忽略科目">
          <el-input v-model="form.crExceptedCoaCodes" type="textarea" placeholder="贷方忽略科目" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleModify()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getcashFlowItem, updateFromDimData, updatecashFlowItem } from '@/api/report'
export default {
  name: 'CashFlow',
  data() {
    return {
      rowType: 1,
      dialogVisible: false,
      form: {
        id: '',
        drCoaCodes: '',
        type: 1,
        crCoaCodes: '',
        drExceptedCoaCodes: '',
        crExceptedCoaCodes: ''
      },
      tableKey: 0,
      tableData: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleSync() {
      this.$confirm('确定要恢复系统默认项目吗?', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateFromDimData().then((res) => {
          if (res.data.errorCode === '0') {
            this.$message.success(res.data.msg)
            this.getData()
          }
        })
      })
    },
    getData() {
      getcashFlowItem().then((res) => {
        this.tableData = res.data.data || []
      })
    },
    handleShow(row) {
      for (const key in this.form) {
        this.form[key] = row[key]
      }
      this.rowType = row.type
      this.dialogVisible = true
    },
    handleModify() {
      updatecashFlowItem(this.form).then((res) => {
        if (res.data.errorCode === '0') {
          this.dialogVisible = false
          this.$message.success(res.data.msg)
          this.getData()
        }
      })
    }
  }
}
</script>
